package com.wrx.server.controller;

import com.wrx.server.pojo.RespBean;
import com.wrx.server.pojo.User;
import com.wrx.server.pojo.UserLoginParam;
import com.wrx.server.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;

/**
 * 用户登录
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody UserLoginParam userLoginParam, HttpServletRequest request){
        return userService.login(userLoginParam.getUsername(),userLoginParam.getPassword(),userLoginParam.getCode(),request);
    }

//    @ApiOperation(value = "获取当前登录用户的信息")
////    @GetMapping("/admin/info")
//    @GetMapping("/user/info")
//    public User getUserInfo(Principal principal) {
//        if (null == principal){
//            return null;
//        }
//        String username = principal.getName();
//        User user = userService.getUserByUserName(username);
//        user.setPassword(null);
//        return user;
//    }


//    @ApiOperation(value = "退出登录")
//    @PostMapping("/logout")
//    public RespBean logout(){
//        return RespBean.success("注销成功！");
//    }
    @ApiOperation(value = "用户注销，清除session")
    @GetMapping("/logout")
    public RespBean logout(HttpSession session, SessionStatus sessionStatus) {
        System.out.println("logout");
        //session失效
        session.invalidate();
        sessionStatus.setComplete();
        return RespBean.success("注销成功");
    }
}
